﻿# -*- coding:utf-8 -*-
from django.forms import ModelForm
from models import Category, Part, PartItem, Document, DocumentItem, DocumentReceipt, DocumentDelivery


#CategoryForm = form_for_model(Category)
class CategoryForm(ModelForm): 
    class Meta:
        model = Category


class PartForm(ModelForm):

    class Meta:
        model = Part
        exclude = ('quantity',)
        
    def save(self):
        self.cleaned_data['quantity'] = 0
        return super(PartForm, self).save()    


class DocumentForm(ModelForm):
    class Meta:
        model = Document
        exclude = ('kind', 'user', 'state',)
        
    def __init__(self, author, kind, *args, **kwargs):
        super(DocumentForm, self).__init__(*args, **kwargs)
        self.author = author 
        self.kind = kind
        
    def save(self):
        self.cleaned_data['user'] = self.author
        self.cleaned_data['kind'] = self.kind
        self.cleaned_data['state'] = 'N'
        return super(DocumentForm, self).save()
 
        

class DocumentItemForm(ModelForm):
    class Meta:    
        model = DocumentItem
        exclude = ('document', 'part',)
    
    def __init__(self, document, part, *args, **kwargs):
        super(DocumentItemForm, self).__init__(*args, **kwargs)
        self.document = document
        self.part = part

    def save(self):
        self.cleaned_data['document'] = self.document
        self.cleaned_data['part'] = self.part
        return super(DocumentItemForm, self).save()        
        
'''
class PartDocumentForm(ModelForm):
    class Meta:
        model = PartDocument
        exclude = ('document', 'part',)

    def __init__(self, part_id, document, *args, **kwargs):
        super(PartDocumentForm, self).__init__(*args, **kwargs)
        self.part_id = part_id 
        self.doc = document
        
    def save(self, commit=True):
        self.cleaned_data['document'] = self.doc
        self.cleaned_data['part'] = self.part_id
        return super(PartDocumentForm, self).save(commit)
'''        
        
class DocumentReceiptForm(ModelForm):
    class Meta:
        model = DocumentReceipt
        exclude = ('document',)
        
    def __init__(self, document, *args, **kwargs):
        super(DocumentReceiptForm, self).__init__(*args, **kwargs)
        self.doc = document
        
    def save(self, commit=True):
        self.cleaned_data['document'] = self.doc
        return super(DocumentReceiptForm, self).save(commit)        
        
        
class DocumentDeliveryForm(ModelForm):
    class Meta:
        model = DocumentDelivery
        exclude = ('document',)
        
    def __init__(self, document, *args, **kwargs):
        super(DocumentDeliveryForm, self).__init__(*args, **kwargs)
        self.doc = document
        
    def save(self, commit=True):
        self.cleaned_data['document'] = self.doc
        return super(DocumentDeliveryForm, self).save(commit)           
       

 